<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="" xml:lang="">
  <head>
    <meta charset="utf-8" />
    <meta name="generator" content="pandoc" />
    <meta
      name="viewport"
      content="width=device-width, initial-scale=1.0, user-scalable=yes"
    />
    <title>interpreter</title>
    <style type="text/css">
      code {
        white-space: pre-wrap;
      }
      span.smallcaps {
        font-variant: small-caps;
      }
      span.underline {
        text-decoration: underline;
      }
      div.column {
        display: inline-block;
        vertical-align: top;
        width: 50%;
      }
    </style>
  </head>
  <body>
    <h3 id="navigation">Navigation</h3>
    <ul>
      <li>
        <a href="https://docs.python.org/3/genindex.html" title="General Index"
          >index</a
        >
      </li>
      <li>
        <a
          href="https://docs.python.org/3/py-modindex.html"
          title="Python Module Index"
          >modules</a
        >
        |
      </li>
      <li>
        <a
          href="introduction.html"
          title="3. An Informal Introduction to Python"
          >next</a
        >
        |
      </li>
      <li>
        <a href="appetite.html" title="1. Whetting Your Appetite">previous</a> |
      </li>
      <li><img src="../_static/py.png" /></li>
      <li><a href="https://www.python.org/">Python</a> »</li>
      <li>
        <a href="https://docs.python.org/3/index.html">3.9.5 Documentation</a> »
      </li>
      <li><a href="index.html">The Python Tutorial</a> »</li>
      <li><div class="line-block"></div></li>
    </ul>
    <p><span id="tut-using"></span></p>
    <h1 id="using-the-python-interpreter">
      <span class="section-number">2. </span>Using the Python Interpreter<a
        href="#using-the-python-interpreter"
        class="headerlink"
        title="Permalink to this headline"
        >¶</a
      >
    </h1>
    <p><span id="tut-invoking"></span></p>
    <h2 id="invoking-the-interpreter">
      <span class="section-number">2.1. </span>Invoking the Interpreter<a
        href="#invoking-the-interpreter"
        class="headerlink"
        title="Permalink to this headline"
        >¶</a
      >
    </h2>
    <p>
      The Python interpreter is usually installed as
      <code>/usr/local/bin/python3.9</code> on those machines where it is
      available; putting <code>/usr/local/bin</code> in your Unix shell’s search
      path makes it possible to start it by typing the command:
    </p>
    <pre><code>python3.9</code></pre>
    <p>
      to the shell.
      <a href="#id2" id="id1" class="footnote-reference brackets">1</a> Since
      the choice of the directory where the interpreter lives is an installation
      option, other places are possible; check with your local Python guru or
      system administrator. (E.g., <code>/usr/local/python</code> is a popular
      alternative location.)
    </p>
    <p>
      On Windows machines where you have installed Python from the
      <a
        href="https://docs.python.org/3/using/windows.html#windows-store"
        class="reference internal"
        ><span class="std std-ref">Microsoft Store</span></a
      >, the <code>python3.9</code> command will be available. If you have the
      <a
        href="https://docs.python.org/3/using/windows.html#launcher"
        class="reference internal"
        ><span class="std std-ref">py.exe launcher</span></a
      >
      installed, you can use the <code>py</code> command. See
      <a
        href="https://docs.python.org/3/using/windows.html#setting-envvars"
        class="reference internal"
        ><span class="std std-ref"
          >Excursus: Setting environment variables</span
        ></a
      >
      for other ways to launch Python.
    </p>
    <p>
      Typing an end-of-file character (Control-D on Unix, Control-Z on Windows)
      at the primary prompt causes the interpreter to exit with a zero exit
      status. If that doesn’t work, you can exit the interpreter by typing the
      following command: <code>quit()</code>.
    </p>
    <p>
      The interpreter’s line-editing features include interactive editing,
      history substitution and code completion on systems that support the
      <a
        href="https://tiswww.case.edu/php/chet/readline/rltop.html"
        class="reference external"
        >GNU Readline</a
      >
      library. Perhaps the quickest check to see whether command line editing is
      supported is typing Control-P to the first Python prompt you get. If it
      beeps, you have command line editing; see Appendix
      <a href="interactive.html#tut-interacting" class="reference internal"
        ><span class="std std-ref"
          >Interactive Input Editing and History Substitution</span
        ></a
      >
      for an introduction to the keys. If nothing appears to happen, or if
      <code>^P</code> is echoed, command line editing isn’t available; you’ll
      only be able to use backspace to remove characters from the current line.
    </p>
    <p>
      The interpreter operates somewhat like the Unix shell: when called with
      standard input connected to a tty device, it reads and executes commands
      interactively; when called with a file name argument or with a file as
      standard input, it reads and executes a <em>script</em> from that file.
    </p>
    <p>
      A second way of starting the interpreter is
      <code>python -c command [arg] ...</code>, which executes the statement(s)
      in <em>command</em>, analogous to the shell’s
      <a
        href="https://docs.python.org/3/using/cmdline.html#cmdoption-c"
        class="reference internal"
        ><code class="xref std std-option docutils literal notranslate"
          >-c</code
        ></a
      >
      option. Since Python statements often contain spaces or other characters
      that are special to the shell, it is usually advised to quote
      <em>command</em> in its entirety with single quotes.
    </p>
    <p>
      Some Python modules are also useful as scripts. These can be invoked using
      <code>python -m module [arg] ...</code>, which executes the source file
      for <em>module</em> as if you had spelled out its full name on the command
      line.
    </p>
    <p>
      When a script file is used, it is sometimes useful to be able to run the
      script and enter interactive mode afterwards. This can be done by passing
      <a
        href="https://docs.python.org/3/using/cmdline.html#cmdoption-i"
        class="reference internal"
        ><code class="xref std std-option docutils literal notranslate"
          >-i</code
        ></a
      >
      before the script.
    </p>
    <p>
      All command line options are described in
      <a
        href="https://docs.python.org/3/using/cmdline.html#using-on-general"
        class="reference internal"
        ><span class="std std-ref">Command line and environment</span></a
      >.
    </p>
    <p><span id="tut-argpassing"></span></p>
    <h3 id="argument-passing">
      <span class="section-number">2.1.1. </span>Argument Passing<a
        href="#argument-passing"
        class="headerlink"
        title="Permalink to this headline"
        >¶</a
      >
    </h3>
    <p>
      When known to the interpreter, the script name and additional arguments
      thereafter are turned into a list of strings and assigned to the
      <code>argv</code> variable in the <code>sys</code> module. You can access
      this list by executing <code>import sys</code>. The length of the list is
      at least one; when no script and no arguments are given,
      <code>sys.argv[0]</code> is an empty string. When the script name is given
      as <code>'-'</code> (meaning standard input), <code>sys.argv[0]</code> is
      set to <code>'-'</code>. When
      <a
        href="https://docs.python.org/3/using/cmdline.html#cmdoption-c"
        class="reference internal"
        ><code class="xref std std-option docutils literal notranslate"
          >-c</code
        ></a
      >
      <em>command</em> is used, <code>sys.argv[0]</code> is set to
      <code>'-c'</code>. When
      <a
        href="https://docs.python.org/3/using/cmdline.html#cmdoption-m"
        class="reference internal"
        ><code class="xref std std-option docutils literal notranslate"
          >-m</code
        ></a
      >
      <em>module</em> is used, <code>sys.argv[0]</code> is set to the full name
      of the located module. Options found after
      <a
        href="https://docs.python.org/3/using/cmdline.html#cmdoption-c"
        class="reference internal"
        ><code class="xref std std-option docutils literal notranslate"
          >-c</code
        ></a
      >
      <em>command</em> or
      <a
        href="https://docs.python.org/3/using/cmdline.html#cmdoption-m"
        class="reference internal"
        ><code class="xref std std-option docutils literal notranslate"
          >-m</code
        ></a
      >
      <em>module</em> are not consumed by the Python interpreter’s option
      processing but left in <code>sys.argv</code> for the command or module to
      handle.
    </p>
    <p><span id="tut-interactive"></span></p>
    <h3 id="interactive-mode">
      <span class="section-number">2.1.2. </span>Interactive Mode<a
        href="#interactive-mode"
        class="headerlink"
        title="Permalink to this headline"
        >¶</a
      >
    </h3>
    <p>
      When commands are read from a tty, the interpreter is said to be in
      <em>interactive mode</em>. In this mode it prompts for the next command
      with the <em>primary prompt</em>, usually three greater-than signs
      (<code>&gt;&gt;&gt;</code>); for continuation lines it prompts with the
      <em>secondary prompt</em>, by default three dots (<code>...</code>). The
      interpreter prints a welcome message stating its version number and a
      copyright notice before printing the first prompt:
    </p>
    <pre><code>$ python3.9
Python 3.9 (default, June 4 2019, 09:25:04)
[GCC 4.8.2] on linux
Type &quot;help&quot;, &quot;copyright&quot;, &quot;credits&quot; or &quot;license&quot; for more information.
&gt;&gt;&gt;</code></pre>
    <p>
      Continuation lines are needed when entering a multi-line construct. As an
      example, take a look at this
      <a
        href="https://docs.python.org/3/reference/compound_stmts.html#if"
        class="reference internal"
        ><code class="xref std std-keyword docutils literal notranslate"
          >if</code
        ></a
      >
      statement:
    </p>
    <pre><code>&gt;&gt;&gt; the_world_is_flat = True
&gt;&gt;&gt; if the_world_is_flat:
...     print(&quot;Be careful not to fall off!&quot;)
...
Be careful not to fall off!</code></pre>
    <p>
      For more on interactive mode, see
      <a href="appendix.html#tut-interac" class="reference internal"
        ><span class="std std-ref">Interactive Mode</span></a
      >.
    </p>
    <p><span id="tut-interp"></span></p>
    <h2 id="the-interpreter-and-its-environment">
      <span class="section-number">2.2. </span>The Interpreter and Its
      Environment<a
        href="#the-interpreter-and-its-environment"
        class="headerlink"
        title="Permalink to this headline"
        >¶</a
      >
    </h2>
    <p><span id="tut-source-encoding"></span></p>
    <h3 id="source-code-encoding">
      <span class="section-number">2.2.1. </span>Source Code Encoding<a
        href="#source-code-encoding"
        class="headerlink"
        title="Permalink to this headline"
        >¶</a
      >
    </h3>
    <p>
      By default, Python source files are treated as encoded in UTF-8. In that
      encoding, characters of most languages in the world can be used
      simultaneously in string literals, identifiers and comments — although the
      standard library only uses ASCII characters for identifiers, a convention
      that any portable code should follow. To display all these characters
      properly, your editor must recognize that the file is UTF-8, and it must
      use a font that supports all the characters in the file.
    </p>
    <p>
      To declare an encoding other than the default one, a special comment line
      should be added as the <em>first</em> line of the file. The syntax is as
      follows:
    </p>
    <pre><code># -*- coding: encoding -*-</code></pre>
    <p>
      where <em>encoding</em> is one of the valid
      <a
        href="https://docs.python.org/3/library/codecs.html#module-codecs"
        class="reference internal"
        title="codecs: Encode and decode data and streams."
        ><code class="sourceCode python">codecs</code></a
      >
      supported by Python.
    </p>
    <p>
      For example, to declare that Windows-1252 encoding is to be used, the
      first line of your source code file should be:
    </p>
    <pre><code># -*- coding: cp1252 -*-</code></pre>
    <p>
      One exception to the <em>first line</em> rule is when the source code
      starts with a
      <a href="appendix.html#tut-scripts" class="reference internal"
        ><span class="std std-ref">UNIX “shebang” line</span></a
      >. In this case, the encoding declaration should be added as the second
      line of the file. For example:
    </p>
    <pre><code>#!/usr/bin/env python3
# -*- coding: cp1252 -*-</code></pre>
    <p>Footnotes</p>
    <p>
      <span class="brackets"><a href="#id1" class="fn-backref">1</a></span
      ><br />
      On Unix, the Python 3.x interpreter is by default not installed with the
      executable named <code>python</code>, so that it does not conflict with a
      simultaneously installed Python 2.x executable.
    </p>
    <h3 id="table-of-contents">
      <a href="https://docs.python.org/3/contents.html">Table of Contents</a>
    </h3>
    <ul>
      <li>
        <a href="#" class="reference internal"
          >2. Using the Python Interpreter</a
        >
        <ul>
          <li>
            <a href="#invoking-the-interpreter" class="reference internal"
              >2.1. Invoking the Interpreter</a
            >
            <ul>
              <li>
                <a href="#argument-passing" class="reference internal"
                  >2.1.1. Argument Passing</a
                >
              </li>
              <li>
                <a href="#interactive-mode" class="reference internal"
                  >2.1.2. Interactive Mode</a
                >
              </li>
            </ul>
          </li>
          <li>
            <a
              href="#the-interpreter-and-its-environment"
              class="reference internal"
              >2.2. The Interpreter and Its Environment</a
            >
            <ul>
              <li>
                <a href="#source-code-encoding" class="reference internal"
                  >2.2.1. Source Code Encoding</a
                >
              </li>
            </ul>
          </li>
        </ul>
      </li>
    </ul>
    <h4 id="previous-topic">Previous topic</h4>
    <p>
      <a href="appetite.html" title="previous chapter"
        ><span class="section-number">1. </span>Whetting Your Appetite</a
      >
    </p>
    <h4 id="next-topic">Next topic</h4>
    <p>
      <a href="introduction.html" title="next chapter"
        ><span class="section-number">3. </span>An Informal Introduction to
        Python</a
      >
    </p>
    <h3 id="this-page">This Page</h3>
    <ul>
      <li><a href="https://docs.python.org/3/bugs.html">Report a Bug</a></li>
      <li>
        <a
          href="https://github.com/python/cpython/blob/3.9/Doc/tutorial/interpreter.rst"
          >Show Source</a
        >
      </li>
    </ul>
    <h3 id="navigation-1">Navigation</h3>
    <ul>
      <li>
        <a href="https://docs.python.org/3/genindex.html" title="General Index"
          >index</a
        >
      </li>
      <li>
        <a
          href="https://docs.python.org/3/py-modindex.html"
          title="Python Module Index"
          >modules</a
        >
        |
      </li>
      <li>
        <a
          href="introduction.html"
          title="3. An Informal Introduction to Python"
          >next</a
        >
        |
      </li>
      <li>
        <a href="appetite.html" title="1. Whetting Your Appetite">previous</a> |
      </li>
      <li><img src="../_static/py.png" /></li>
      <li><a href="https://www.python.org/">Python</a> »</li>
      <li>
        <a href="https://docs.python.org/3/index.html">3.9.5 Documentation</a> »
      </li>
      <li><a href="index.html">The Python Tutorial</a> »</li>
      <li><div class="line-block"></div></li>
    </ul>
    <p>
      ©
      <a href="https://docs.python.org/3/copyright.html">Copyright</a>
      2001-2021, Python Software Foundation.<br />
      The Python Software Foundation is a non-profit corporation.
      <a href="https://www.python.org/psf/donations/">Please donate.</a>
    </p>
    <p>
      Last updated on May 30, 2021.
      <a href="https://docs.python.org/3/bugs.html">Found a bug</a>?<br />
      Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 2.4.4.
    </p>
  </body>
</html>
